package com.cmd.driver;


import com.alibaba.fastjson.JSONObject;
import com.mgr.DriveRecordMgr;
import com.mgr.UserOrderMgr;
import com.player.DriverUser;
import com.player.User;
import com.qy.bean.OrderInfo;
import com.qy.util.StaticData;
import com.util.AbstractCommand;
import com.util.PBMessage;

/**
 * 
 * 司机还车到达公司
 * @author zwl
 * 2018年2月1日
 */
public class OrderArriveSystemCMD  extends AbstractCommand{

	@Override
	public void execute(User user, PBMessage packet) throws Exception {
		JSONObject reJs = new JSONObject();
		reJs.put(StaticData.reKey.cmd.getKey(),packet.getCode());
		JSONObject param = packet.getMessage();
		if(!param.containsKey("point")||!param.containsKey("orderId")){
			reJs.put(StaticData.reKey.code.getKey(), StaticData.codeType.param_error.getType());
			user.sendPacket(reJs);
			return;
		}
		DriverUser driverUser = (DriverUser) user;
		if(driverUser.getServerOrder()==null){
			reJs.put(StaticData.reKey.code.getKey(), StaticData.codeType.NOT_END_SEVER.getType());
			user.sendPacket(reJs);
			return; 
		}
		// 订单号
		int orderId = param.getInteger("orderId");
		OrderInfo info = UserOrderMgr.getOrderInfo(orderId);
		if (info == null
			||info.getOrderStatus() != StaticData.OrderStatus.ARRIVE_COUNT_DOWN.getType()) {
		    reJs.put(StaticData.reKey.code.getKey(),StaticData.codeType.ORDER_not_exist.getType());
		    user.sendPacket(reJs);
		    return;
		}
		String myPoint = param.getString("point");
		/*
		 * 更新司机的坐标
		 */
		driverUser.setMyPoint(myPoint);
		reJs.put(StaticData.reKey.code.getKey(), StaticData.codeType.ok.getType());
		user.sendPacket(reJs);
		
		/*
		 * 更新司机的坐标
		 */
		new DriverMoveCMD().execute(driverUser, packet);
		//司机端行程完毕
		DriveRecordMgr.completOd(driverUser.getServerOrder().getId());
		return;
	}

}
